package com.yunquan.ohana.ui.calendar.lists

import android.os.Bundle
import androidx.core.graphics.toColorInt
import androidx.databinding.ObservableField
import com.google.gson.Gson
import com.yunquan.ohana.R
import com.yunquan.ohana.base.binding.command.BindingAction
import com.yunquan.ohana.base.binding.command.BindingCommand
import com.yunquan.ohana.base.ui.AppManager
import com.yunquan.ohana.base.ui.ItemViewModel
import com.yunquan.ohana.entity.ListsEntity
import com.yunquan.ohana.ui.calendar.event.week.dialog.BottomDeleteDialog
import com.yunquan.ohana.ui.calendar.lists.list.ListsListActivity
import com.yunquan.ohana.utils.Utils

class ListsItemViewModel(
    viewModel: ListsViewModel,
    val data: ListsEntity,
    edit: Boolean = false
) : ItemViewModel<ListsViewModel>(viewModel) {
    val name = ObservableField<String>(data.name)
    val color = ObservableField(data.getColorData().normal.toColorInt())
    val count = ObservableField("${data.quantityToBeCompleted} items")
    val isEdit = ObservableField(edit)
    val checked = ObservableField(false)

    val onDeleteClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            val deleteDialog = BottomDeleteDialog(AppManager.appManager.currentActivity()!!)
            deleteDialog.setTitle(Utils.instance.context.getString(R.string.delete_lists_group, 1))
            deleteDialog.setTip(Utils.instance.getString(R.string.tip_delete_lists_group))
            deleteDialog.setOnDeleteClick { dialog ->
                viewModel.deleteLists(arrayListOf(data))
            }
            deleteDialog.show()
        }
    })

    val onItemClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            if (isEdit.get() == true) {
                val b = checked.get() ?: false
                checked.set(!b)
            } else {
                val bundle = Bundle()
                bundle.putString("deviceId", viewModel.mDeviceId)
                bundle.putString("data", Gson().toJson(data))
                viewModel.startActivityResult(ListsListActivity::class.java, bundle)
            }
        }
    })
}